class Solution:
    def reverseWords(self, s):
        l=len(s)-1
        index=0
        lst=[]
        while l!=-1 and s[l]==' ':
            l-=1
        while l!=-1:
            if s[l]==' ' and s[l+1]!=' ':
                tmp=l+1
                while tmp!=len(s) and s[tmp]!=' ':
                    lst.append(s[tmp])
                    tmp+=1
                lst.append(' ')
            elif l==0:
                tmp=0
                while tmp!=len(s) and s[tmp]!=' ':
                    lst.append(s[tmp])
                    tmp+=1
            l-=1
        return "".join(lst).strip()

def main():
    sol=Solution()
    s="  the sky is blue"
    res=sol.reverseWords(s)
    print(res)

main()